import {toJS} from "mobx";
import { WeaLocaleProvider } from 'ecCom';
import isMobile from '../../../../../utils/mobileDetect';

const { getLabel } = WeaLocaleProvider;

export default {
  custom: {
    validate: com => {
      const { value, config: { coms }} = com;
      const errors = [];

      if (!coms.length) return true;

      value.forEach((rowVal, rowIndex) => {
        errors[rowIndex] = errors[rowIndex] || [];

        if (!com.rules.length) return;

        rowVal.forEach((colVal, colIndex) => {
          const com = coms[colIndex];
          if(!(com  && com.rules))return;
          com.rules.forEach(rule => {
            const newCom = { ...com, value: colVal };
            const isValid = rule.validate(newCom);

            if (isValid) return;

            errors[rowIndex][colIndex] = rule.error(newCom);
          });
        })
      });

      com.config.errors = errors;

      return !errors.toString().replace(/,/g, '');
    },
    error: () => {
      if (isMobile()) {
        return getLabel('520678', '存在不符合规则的字段');
      } else {
        return '';
      }
    }
  }
}